<?php
/**
 * 获取微博信息做缓存
 * @author yangchao	

 * @package mblog
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode utf-8
 */




define("CACHE_API_MBLOG_CACHE","_api_mblog_");
define("CACHE_TIME_API_MBLOG_CACHE",	CACHE_EXPIRE_MIN_5);


class mblog_cache_v2 
{
	
	//增加取微博用户信息的缓存
	function cache_api_get_user_info_by_uids($uids="",$screen_name="",$trim_status=0,$has_extend=0)
	{
		if(!$uids && !$screen_name)	
		{
			return false;
		}
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_uids-{$uids}-{$screen_name}-{$trim_status}-{$has_extend}";
		
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$content = $mblog_from_api->api_get_user_info_by_uids($uids, $screen_name, $trim_status, $has_extend, 0);
			
			if($content["data"]["errno"] == 1)
			{
				$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_1 * 2);
			}
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	/**
	 * 批量获取用户的微博信息返回格式化后数据
	 * @param unknown_type $uids
	 * @param unknown_type $screen_name
	 * @param unknown_type $trim_status
	 * @param unknown_type $has_extend
	 * @return array[uid] = array();
	 */
	function cache_api_get_user_info_by_uids_format($uids="",$screen_name="",$trim_status=0,$has_extend=0)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_uids_format-{$uids}-{$screen_name}-{$trim_status}-{$has_extend}";
	
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$content = $mblog_from_api->api_get_user_info_by_uids($uids, $screen_name, $trim_status, $has_extend, 0);
				
			if($content["data"]["errno"] == 1)
			{
				$_data = array();
				foreach ($content['data']['result'] as $value)
				{
					$_data[$value['idstr']] = $value;
				}
				if(!empty($_data))
				{
					$mc->set($key, serialize($_data), 0, CACHE_EXPIRE_MIN_1 * 2);
					$content = $_data;
				}
			}
		}
		else
		{
			$content = unserialize($cache);
		}
	
		return $content;
	}
	
	/**
	 * 根据单个uid获取微博用户信息
	 * @param unknown_type $uid
	 * @param unknown_type $screen_name
	 * @param unknown_type $has_extend
	 * @return mixed
	 */
	function cache_api_get_user_info_by_uid($uid="",$screen_name="",$has_extend=0)
	{
	    $key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_uid-{$uid}-{$screen_name}-{$has_extend}";
	    $mc = mem_cache::get_instance();
	    $cache = $mc->get($key,true);
	    if ($cache === false)
	    {
	        $mblog_from_api = new mblog_from_api();
	        $content = $mblog_from_api->api_get_user_info_by_uid($uid, $screen_name, $has_extend, 901);
	        
	        if($content["data"]["errno"] != 1)
	        {
	        	//如果返回状态不正常 ，不缓存这个用户的信息
	        }
	        else
	        {
	        	$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_1 * 2);
	        }
            
	    }
	    else
	    {
	        $content = unserialize($cache);
	    }
	
	    return $content;
	}
	
	/**
	 * 根据域名获取微博用户信息
	 * @param unknown_type $domain
	 * @return mixed
	 * 
	 * @author zhangchao
	 */
	function cache_api_get_user_info_by_domain($domain)
	{
	    $key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_domain-{$domain}";
	    $mc = mem_cache::get_instance();
	    $cache = $mc->get($key,true);
	    if ($cache === false)
	    {
	        $mblog_from_api = new mblog_from_api();
	        $content = $mblog_from_api->api_get_user_info_by_domain($domain, 901);
	        $mc->set($key, serialize($content), 0, CACHE_TIME_API_MBLOG_CACHE);
	    }
	    else
	    {
	        $content = unserialize($cache);
	    }
	
	    return $content;
	}
	
	/**
	 * 获取感兴趣的人
	 * @return mixed
	 *
	 * @author shaocong
	 */
	function cache_api_get_guess_user_like_call_with_cookie($p=1,$pcount=10,$site_id)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_guess_user_like_call_with_cookie-{$p}-{$pcount}-{$site_id}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$content = $mblog_from_api->api_guess_user_like_call_with_cookie($p=1,$pcount,$site_id);
			if(! empty($content['data']['result']))
			{
				$mc->set($key, serialize($content), 0, CACHE_TIME_API_MBLOG_CACHE);
			}
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}	
	
	
	/**获取互粉UID列表的缓存 分页
	 uid 	true 	int64 	需要获取双向关注列表的用户ID
	pcount 	false 	int 	返回结果条数数量，默认2000
	p 	false 	int 	返回结果的页码
	start_time 	false 	long 	某时间段的起始时刻，取值为时间戳
	end_time 	false 	long 	某时间段的终止时刻，取值为时间戳
	*/
	function cache_api_get_user_bilateral_ids_by_uid_list($uid, $p=1, $pcount=2000, $start_time=0, $end_time=0)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_uids-{$uid}-{$p}-{$pcount}-{$start_time}-{$end_time}";
		
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$content = $mblog_from_api->api_get_user_bilateral_ids_by_uid($uid, $p, $pcount, $start_time, $end_time, 901);
				
			if($content["data"]["errno"] == 1)
			{
				$mc->set($key, serialize($content), 0, CACHE_TIME_API_MBLOG_CACHE);
			}
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;		
	}
	
    
	/**
	 * 获取所有的互粉ID
	 * @param int $uid
	 * @return mixed
	 */
	function cache_api_get_user_bilateral_ids_by_uid_all($uid)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_info_by_uids-{$uid}";
	
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$aFans = $mblog_from_api->api_get_user_bilateral_ids_by_uid($uid,1,1,0,0,901);
			if($aFans["data"]["count"] >= 1)
			{
				$allFans = array();
				$count = $aFans["data"]["count"];
				$totalPage = ceil($count/2000);
				for ($i=1; $i<=$totalPage; $i++)
				{
					$_fans = $mblog_from_api->api_get_user_bilateral_ids_by_uid($uid, $i, 2000, 0,0,901);
					if(!empty($_fans["data"]["result"]))
					{
						foreach ($_fans["data"]["result"] as $val)
						{
							$allFans[] = $val;
						}
					}
				}
				if(!empty($allFans))
				{
					$mc->set($key, serialize($allFans), 0, CACHE_TIME_API_MBLOG_CACHE);
				}
				else
				{
					$allFans = array();
				}
			}
			else
			{
				$allFans = array();
			}
		}
		else
		{
			$allFans = unserialize($cache);
		}
		return $allFans;
	}

	/**
	 * 获取所有的粉丝ID  暂时只能支持获取5000条数据
	 * @param int $uid
	 * @return mixed
	 */
	function cache_api_get_user_followers_ids_by_uid_all($uid)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_followers_ids_by_uid_all-{$uid}";
	
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
	
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$aFans = $mblog_from_api->api_get_user_followers_ids_by_uid($uid,"",1,1,-1,"","",901);
			$allFans = array();
			
			//暂时先取出5000条数据
			$aFans["data"]["count"] = 4999;
			if($aFans["data"]["count"] >= 5000)
			{
				$next_cursor = -1;
				$i = 1;
				while ( $next_cursor !=0 && $next_cursor<="10000")
				{   
					$_fans = $mblog_from_api->api_get_user_followers_ids_by_uid($uid,"",$i,5000, $next_cursor,"","",901);
					var_dump($_fans);die();
					if(!empty($_fans["data"]["result"]))
					{
						foreach ($_fans["data"]["result"] as $val)
						{
							$allFans[] = $val;
						}
					}
					$next_cursor = $_fans["data"]["next_cursor"];
					$i++;
				}
			}
			else
			{
				$_fans = $mblog_from_api->api_get_user_followers_ids_by_uid($uid,"","",5000,-1,"","",901);
				if(!empty($_fans["data"]["result"]))
				{
					foreach ($_fans["data"]["result"] as $val)
					{
						$allFans[] = $val;
					}
				}
			}
			
			if(!empty($allFans))
			{
				$mc->set($key, serialize($allFans), 0, CACHE_TIME_API_MBLOG_CACHE);
			}
		}
		else
		{
				$allFans = unserialize($cache);
		}
		return $allFans;
	}
	
	/**
	 * 获取互粉用户列表
	 * @param unknown_type $uid
	 * @param unknown_type $screen_name
	 * @param unknown_type $has_extend
	 * @return mixed
	 */
	function cache_api_get_user_bilateral_by_uid($uid)
	{
	    $limit = 600;
	    $data = array();
	    
	    $key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_bilateral_by_uid-{$uid}";
	    $mc = mem_cache::get_instance();
	    $cache = $mc->get($key,true);
   		if ($cache === false)
	    {
	        $mblog_from_api = new mblog_from_api();
	        $content = $mblog_from_api->api_get_user_bilateral_by_uid($uid,1,1,0,0,901);
	        if($content["data"]["count"] > 200)
	        {
	            $max_page = ceil($content["data"]["count"]/200)>ceil($limit/200) ? ceil($limit/200) : ceil($content["data"]["count"]/200);
	            for ($i = 0; $i < $max_page; $i++) {
	                $content = $mblog_from_api->api_get_user_bilateral_by_uid($uid, ($i+1),200,0,0,901);
	                if(!empty($content["data"]["result"]))
	                {
	                    $data = array_merge($data, $content["data"]["result"]);
	                }
	            }
	        }
	        else
	        {
	            $content = $mblog_from_api->api_get_user_bilateral_by_uid($uid,1,200,0,0,901);
	            $data = $content["data"]["result"];
	        }
	        $mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_1 * 2);
	    }
	    else
	    {
	        $data = unserialize($cache);
	    }
	
	    return $data;
	}
	
	/**
	 * 获取我关注的用户列表
	 * @param unknown_type $uid
	 * @param unknown_type $screen_name
	 * @param unknown_type $has_extend
	 * @return mixed
	 */
	function cache_api_get_user_friends_by_uid($uid="",$screen_name="")
	{
	    $limit = 600;
	    $data = array();
	    
	    $key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_friends_by_uid-{$uid}-{$screen_name}";
	    $mc = mem_cache::get_instance();
	    $cache = $mc->get($key,true);
    	if ($cache === false)
	    {
	        $mblog_from_api = new mblog_from_api();
	        $content = $mblog_from_api->api_get_user_friends_by_uid($uid,$screen_name,1,1,0,0,0,0,901);
	        if($content["data"]["count"] > 200)
	        {
	            $max_page = ceil($content["data"]["count"]/200)>ceil($limit/200) ? ceil($limit/200) : ceil($content["data"]["count"]/200);
	            for ($i = 0; $i < $max_page; $i++) {
	                $content = $mblog_from_api->api_get_user_friends_by_uid($uid,$screen_name,($i+1),200,0,0,0,0,901);
	                if(!empty($content["data"]["result"]))
	                {
	                    $data = array_merge($data, $content["data"]["result"]);
	                }
	            }
	        }
	        else
	        {
	            $content = $mblog_from_api->api_get_user_friends_by_uid($uid,$screen_name,1,200,0,0,0,0,901);
	            $data = $content["data"]["result"];
	        }
	        $mc->set($key, serialize($data), 0, CACHE_TIME_API_MBLOG_CACHE);
	    }
	    else
	    {
	        $data = unserialize($cache);
	    }
	
	    return $data;
	}
	
	/**
	 * 获取用户所有的tag
	 * @param unknown_type $uid
	 */
	function cache_api_get_user_tags_by_uid($uid)
	{
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_tags_by_uid-{$uid}";
		
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$aTags = $mblog_from_api->api_get_user_tags_by_uid($uid,1,20,0,0,901);
			
			if(!empty($aTags["data"]["result"]))
			{
				$allTags = $aTags["data"]["result"];
			}
			else
			{
				$allTags = array();
			}
			if(!empty($allTags))
			{
				$mc->set($key, serialize($allTags), 0, CACHE_TIME_API_MBLOG_CACHE);
			}
		}
		else
		{
			$allTags = unserialize($cache);
		}
		return $allTags;
	}
	
	/**
	 * 获取表情
	 * 套缓存
	 * @param unknown_type $type
	 * @param unknown_type $language
	 * @return mixed
	 * @author zhangchao
	 */
	function cache_api_get_mblog_emotions($type="face",$language="cnname")
	{
		global $config;
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_mblog_emotions-{$type}-{$language}";
		
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$list = $mblog_from_api->api_get_mblog_emotions($type,$language,$config['current_site_id']);
			$mc->set($key, serialize($list), 0, CACHE_TIME_API_MBLOG_CACHE);
		}
		else
		{
			$list = unserialize($cache);
		}
		return $list;
	}
	
	/**
	 * 获取表情数组，用于表情替换
	 * @param unknown_type $type
	 * @param unknown_type $language
	 * @return mixed
	 * @author zhangchao
	 */
	function cache_api_get_mblog_emotions_for_replace()
	{
		global $config;
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_mblog_emotions_for_replace";
	
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$list = $this->cache_api_get_mblog_emotions();
			$new_list = array();
			foreach ($list['data'] as $value) {
				$new_list[$value['phrase']] = "<img src=\"{$value['url']}\" title=\"{$value['phrase']}\" alt=\"{$value['phrase']}\" type=\"face\">";
			}
			$mc->set($key, serialize($new_list), 0, CACHE_TIME_API_MBLOG_CACHE);
		}
		else
		{
			$new_list = unserialize($cache);
		}
		return $new_list;
	}
	
	
	static function return_user_link($matches)
	{
		$url_item	= urlencode($matches[1]);
		return "<a href='/group/user/link.php?nickname=$url_item' target='_balnk'>@$matches[1]</a>";
	}
	
	//替换微博内容里的  @，话题，链接
	function replace_content($content,$replace_face=true,$strip_topic=false,$group_id='')
	{
		
		//替换链接(必须先处理)
		$content = preg_replace("/(?<![>|\"|'])\s*(http:[A-Za-z0-9_\/\.\-]*)/","<a href='\${1}' target='_blank'>\${1}</a>",$content);
		//替换用户链接
		$content = preg_replace_callback('/@([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)/u',"mblog_cache::return_user_link",$content);
		//替换话题标签
		if($strip_topic == 1)
		{
			$content = preg_replace("/#([^@|^ ]*)#/","",$content);
		}
		elseif($strip_topic == false) 
		{
			$content = preg_replace("/#([^@|^ ]*)#/","<a href='/group/topic/link.php?group_id={$group_id}&topic=\${1}' target='_balnk'>#\${1}#</a>",$content);
		}
		
		if($replace_face && preg_match("/\[.*?\]/",$content))
		{
			$content = $this->get_faces($content);
		}
		
		
		return $content;
	}
	

	/**
	 * 替换表情图片
	 */
	function get_faces($content)
	{
		$open_emotions = $this->cache_api_get_mblog_face();
		$open_emotions = $open_emotions["data"];
		$search_array = $open_emotions["search"];	
		$replace_array = $open_emotions["replace"];
		foreach($replace_array as &$row)
		{
			$row	= str_replace("<img ", "<img class='dfz_face_icon'", $row);
		}
		$content = str_replace($search_array,$replace_array,$content);
		
		return $content;
	}
	
	//删除内容里的话题标签
	function delete_topic($content)
	{
		$content= preg_replace("/(#[^@| ]*?#)/","",$content);
		return $content;
	}
	
	
	/**
	 * 获取两个用户的关系
	 * @param string $source_id   当前用户uid
	 * @param string $target_id   目标用户uid
	 * @param int $site_id        站点id
	 * @return mixed
	 * 
	 * @author zhanghuan
	 * @since 2012-03-31
	 */
	function cache_api_get_user_friendships($source_id, $target_id, $site_id)
	{
		$data = array();
		 
		$key = CACHE_API_MBLOG_CACHE . "-cache_api_get_user_friendships-{$source_id}-{$target_id}-{$site_id}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key,true);
		if ($cache === false)
		{
			$mblog_from_api = new mblog_from_api();
			$data = $mblog_from_api->api_get_user_friendships($source_id, $target_id, "", "", $site_id);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}
	
		return $data;
	}
	
	
	
}

